var app = getApp();

Page({
    data: {
      imgList:"",
      list:[],
      categoryList:"",
      categoryId:"",
      selectedTitle: "0",
      newsList:"",
      pageCurrent:1,
      pageSize:6,
      pageNum:"",  //总页数
      imgUrl:app.globalData.imgUrl,
      loading:false,   //"上拉加载中"
      loadingComplete:false   //"加载完成"
    },
    onLoad: function () {
        var that = this;
        that.setData({
           navH: app.globalData.navHeight,
           marH: app.globalData.navHeight+20,
        })
        that.getImg();
        that.getCategoryList();
    },
    onShareAppMessage() {
        return {
          title: '在海花岛这样订房也不错，快看快约！',
          //path: '/pages/news/list/news-list'
          path: 'pages/index/index?url=/pages/news/list/news-list'
        }
    },
    getImg:function(){
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/index/bannerList",
            data: {
                position: "other"
            },
            header: {  
                'content-type': 'application/json' 
            },
            method: "GET",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var imgData = res.data.data;
                    that.setData({
                        imgList: imgData
                    })
                }
            },
            
        });
    },
    getCategoryList:function(){
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/index/categoryList",
            data: {
                type: "default"
            },
            header: {  
                'content-type': 'application/json' 
            },
            method: "GET",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var categoryData = res.data.data;
                    for (var i = 0; i < res.data.data.length; i++) {   //list声明一维
                        that.data.list[i] = [];
                    }
                    that.setData({
                        categoryList: categoryData,
                        categoryId:categoryData[0].id
                    });
                    that.articleList(that.data.categoryId);
                }
            },
            
        });
    },
    articleList:function(categoryId){
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/index/articleList",
            data: {
                page: that.data.pageCurrent,
                pagesize: that.data.pageSize,
                category_id: categoryId
            },
            header: {  
                'content-type': 'application/json' 
            },
            method: "GET",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var articlePageNum = res.data.data.last_page;
                    for (var j = 0; j < res.data.data.data.length; j++) { //list声明二维
                        that.data.list[that.data.selectedTitle].push(res.data.data.data[j]);
                    }
                    var articleData = that.data.list[that.data.selectedTitle];
                    that.setData({
                        newsList: articleData,
                        pageNum: articlePageNum
                    })
                }
            },
            
        });
    },
    tabTit: function (e) {   
        let that = this;
        that.data.categoryId=e.currentTarget.dataset.name;
        that.data.list[e.currentTarget.id]=[];
        that.setData({
           selectedTitle: e.currentTarget.id,
           pageCurrent:1,
           loadingComplete:false,
        });
        that.articleList(that.data.categoryId);
    },
    onReachBottom: function () {
        let that = this;
        if (that.data.pageCurrent < that.data.pageNum) {
            that.setData({
                loading:true,
                loadingComplete:false,
                pageCurrent: that.data.pageCurrent + 1
            })
            that.articleList(that.data.categoryId);
        }else if(that.data.pageCurrent >= that.data.pageNum){
            that.setData({
                loading:false,
                loadingComplete:true
            })
        }
    }
})
